#region System
using System;
using System.Collections.Generic;
using CF.Framework.Engine;
using CF.Framework.Engine.Controls;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Effects;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace PSR.App.Screens
{
	public class MainMenuScreen : GameScreen
	{
		private Background background;
		private TextureBox banner;
		private TextureBox tip;
		private TextureBox startMask;
		private FadeOutEffect startMaskEffect;

		public MainMenuScreen()
		{
		}

		protected override void OnLoadContent()
		{
			base.OnLoadContent();

			//this.AudioManager.Load<Song>(Metadata.Music.Dream);

			startMaskEffect = new FadeOutEffect() { Duration = TimeSpan.FromMilliseconds(500) };
			startMaskEffect.EffectEnd += new EventHandler(startMaskEffect_EffectEnd);

			background = new Background() { Context = Load<Texture2D>("Images/MainMenuBackground"), Opacity = 100, OpacityMask = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, ScreenManager.ScreenHeight, Color.Black) };
			banner = new TextureBox() { Context = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, 50, Color.Black), Position = new Vector2(0, 0), Opacity = 100 };
			tip = new TextureBox() { Context = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, 50, Color.Black), Position = new Vector2(0, ScreenManager.ScreenHeight - 50), Opacity = 100 };

			startMask = new TextureBox() { Context = DrawingUtility.CreateRectangleTexture(ScreenManager.GraphicsDevice, ScreenManager.ScreenWidth, ScreenManager.ScreenHeight, Color.Black) };
			startMask.Effects.Add(startMaskEffect);

			this.Components.Add(background);
			this.Components.Add(banner);
			this.Components.Add(tip);

			this.Components.Add(startMask);
		}

		void startMaskEffect_EffectEnd(object sender, EventArgs e)
		{
			
		}

		private void InitializeMenu()
		{
			// init menu
			Menu menu = new Menu();
			menu.Font = ScreenManager.SpriteFonts.LargeFont;
			menu.Color = Color.Black;
			menu.SelectedColor = new Color(206, 184, 116);
			menu.Direction = MenuDirection.BottomToTop;
			menu.Dock = MenuDock.BottomRight;

			MenuItem newItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.new") };
			MenuItem continueItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.continue") };
			MenuItem customItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.custom") };
			MenuItem highscoreItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.highscore") };
			MenuItem creditItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.credit") };
			MenuItem optionItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.option") };
			MenuItem exitItem = new MenuItem() { Text = GameResources.GetDescription("mainmenu.exit") };

			customItem.Selected += new EventHandler(customItem_Selected);

			menu.Items.Add(newItem);
			menu.Items.Add(continueItem);
			menu.Items.Add(customItem);
			menu.Items.Add(highscoreItem);
			menu.Items.Add(creditItem);
			menu.Items.Add(optionItem);
			menu.Items.Add(exitItem);

			this.Components.Add(menu);

			// init version label
			Label version = new Label();
			version.Text = string.Format(GameResources.GetDescription("mainmenu.version"), Metadata.System.Version);
			version.Font = ScreenManager.SpriteFonts.SmallFont;
			version.Position = new Vector2(10, ScreenManager.ScreenHeight - ScreenManager.SpriteFonts.SmallFont.LineSpacing - 10);

			this.Components.Add(version);
		}

		void customItem_Selected(object sender, EventArgs e)
		{
		}

		protected override void OnReady()
		{
			base.OnReady();

			this.startMaskEffect.Start();
		}

		protected override void OnUpdate(GameTime gameTime)
		{
			base.OnUpdate(gameTime);
		}

		protected override void OnDraw(GameTime gameTime, SpriteBatch spriteBatch)
		{
			base.OnDraw(gameTime, spriteBatch);
		}

		protected override void OnHandleInput(InputState input)
		{
			base.OnHandleInput(input);

			if (input.IsPauseGame(null))
			{
				AudioManager.Stop();
				ScreenController.GotoLogoScreen();

				HandleInput = false;
			}
		}
	}
}
